﻿/*----------------------------------------------------------------
           // Copyright (C) 2008 安华顺通(北京)科技有限公司
           // 版权所有。 
           //
           // 文件名：SysCompilerEdt.cs
           // 文件功能描述：扩展类型编译功能。
           //
           // 
           // 创建标识：周玉偈20080916
           //
           // 修改标识
           // 修改描述：
           //
           // 修改标识：
           // 修改描述：
//----------------------------------------------------------------*/

#region UsingStatements

using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
using System.Windows.Forms;
using Microsoft.CSharp;
using RuleCode.ObjectTree;

#endregion

//namespace RuleCode.ObjectTree.Compile

namespace RuleCode.compiler
{
    public class SysCompilerEdt
    {
        private static SysCompilerEdt sysCompilerEdt;
        private static readonly string EDTPath =Util.GetExtendDateTypesPath +"\\";// Util.OTPath + "\\Module\\ExtendDataTypes\\";
        //private static readonly string keyFile = "D:\\snk\\RuleCode.Query.snk";

        public static SysCompilerEdt Instance
        {
            get
            {
                if (sysCompilerEdt == null)
                {
                    sysCompilerEdt = new SysCompilerEdt();
                }
                return sysCompilerEdt;
            }
        }

        public void compileExtendDataType(SysTreeNode node)
        {
            #region 初始化环境


            node.ExpandAll();

            Util.SetPropertyContent(node);

            string name = node.Text;
            string op;
            CodeCompileUnit CompileUnit = new CodeCompileUnit(); //编译单元容器
            //CompileUnit.AssemblyCustomAttributes.Add(new System.CodeDom.CodeAttributeDeclaration
            //        ("System.Reflection.AssemblyKeyFile", new System.CodeDom.CodeAttributeArgument(
            //        new System.CodeDom.CodePrimitiveExpression(keyFile)))); 
            CodeNamespace typeNameSpace = new CodeNamespace(Util.RootClassNameSpace+".ExtendType"); //命名空间
            CompileUnit.Namespaces.Add(typeNameSpace);
            CodeTypeDeclaration type = new CodeTypeDeclaration(name); //类定义，Name是类名称
            //设置类的继承。
            type.Attributes = MemberAttributes.Public;
            type.IsClass = true;
            type.BaseTypes.Add("RuleCode.Common." + typeNameMapping(((EDTProperty)node.Property).EDTType));

            // 将类型加入到容器的类型集合中
            typeNameSpace.Types.Add(type);


            //命名空间里面的引用，注意：引用在空间里面，单元里面；
            typeNameSpace.Imports.Add(new CodeNamespaceImport("System"));
            typeNameSpace.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
            typeNameSpace.Imports.Add(new CodeNamespaceImport("System.Text"));
            typeNameSpace.Imports.Add(new CodeNamespaceImport("System.ComponentModel"));
            typeNameSpace.Imports.Add(new CodeNamespaceImport("System.Data"));
            typeNameSpace.Imports.Add(new CodeNamespaceImport("RuleCode.Common"));
            //typeNameSpace.Imports.Add(new CodeNamespaceImport("RuleCode.Enum"));
            //typeNameSpace.Imports.Add(new CodeNamespaceImport("RuleCode.Table"));
            //typeNameSpace.Imports.Add(new CodeNamespaceImport("RuleCode.Table.TableField"));
            typeNameSpace.Imports.Add(new CodeNamespaceImport("RuleCode.ObjectTree"));
            //typeNameSpace.Imports.Add(new CodeNamespaceImport("RuleCode.Queries"));
            typeNameSpace.Imports.Add(new CodeNamespaceImport(RuleCode.ObjectTree.Util.RootClassNameSpace+".Enum"));
            typeNameSpace.Imports.Add(new CodeNamespaceImport(RuleCode.ObjectTree.Util.RootClassNameSpace+".Table"));
            typeNameSpace.Imports.Add(new CodeNamespaceImport(RuleCode.ObjectTree.Util.RootClassNameSpace+".Table.TableField"));


            //增加默认构造器
            // Declare a constructor that takes a string argument and calls the base class constructor with it.
            CodeConstructor baseStringConstructor = new CodeConstructor();
            baseStringConstructor.Attributes = MemberAttributes.Public;
            // Calls a base class constructor with the TestStringParameter parameter.
            baseStringConstructor.BaseConstructorArgs.Add(new CodeVariableReferenceExpression());

            string typeComment = string.Empty;
            if (((EDTProperty)node.Property).Extends != null && ((EDTProperty)node.Property).Extends != "")
                typeComment = "Extend From edt :" + ((EDTProperty)node.Property).Extends;

            if (((EDTProperty)node.Property).EDTType == "TEnum")
            {
                StringBuilder sBuilder = new StringBuilder();
                string enumType = string.Empty;
                if (((EDTEnumProperty)node.Property).EnumType != "" && ((EDTEnumProperty)node.Property).EnumType != null)
                    enumType = ((EDTEnumProperty)node.Property).EnumType;
                else
                    enumType = ((EDTEnumProperty)node.Property).Name;
                typeComment = "EnumType is " + enumType;

                sBuilder.AppendFormat("this.BaseEnumType =typeof("+Util.RootClassNameSpace+".Enum.{0});",
                                      enumType);
                CodeSnippetStatement statement = new CodeSnippetStatement(sBuilder.ToString());
                baseStringConstructor.Statements.Add(statement);
            }
            typeComment = ((EDTProperty)node.Property).EDTType + ", " + typeComment;

            type.Comments.Add(new CodeCommentStatement("<summary>", true));
            type.Comments.Add(new CodeCommentStatement(typeComment, true));
            type.Comments.Add(new CodeCommentStatement("</summary>", true));

            // Adds the constructor to the Members collection of the DerivedType.
            type.Members.Add(baseStringConstructor);


            EDTProperty edtProperty;

            #endregion

            //还差一个Int64类型，扩展类型还差一个Enum

            #region C# Tstring

            if (((EDTProperty)node.Property).EDTType == "TString")
            {
                //增加带参数构造器
                // Declare a constructor that takes a string argument and calls the base class constructor with it.
                CodeConstructor baseStringConstructor1 = new CodeConstructor();
                baseStringConstructor1.Attributes = MemberAttributes.Public;
                // Declares a parameter of type string named "TestStringParameter".    
                baseStringConstructor1.Parameters.Add(new CodeParameterDeclarationExpression("System.String", "_" + name));
                // Calls a base class constructor with the TestStringParameter parameter.
                baseStringConstructor1.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("_" + name));
                // Adds the constructor to the Members collection of the DerivedType.
                type.Members.Add(baseStringConstructor1);

                //定义隐式转换
                op = @"
                public static implicit operator " + name + @" (string x) { return new " + name +
                     @"(x); }
                public static explicit operator string(" + name +
                     @" x) {  return x.Value; }
                public static explicit operator bool(" + name +
                     @" x) { return x.Value== DefaultValue.Value; }
                ";
                type.Members.Add(new CodeSnippetTypeMember(op));
                //属性变量
                edtProperty = ((EDTProperty)node.Property);
                op =
                    @"
                    public override  EDTStringProperty Property
                    {
                        get
                        {
                            if (this.property == null)
                            {
                                this.property = new EDTStringProperty();
                            ";

                #region C# 定义属性

                //定义属性类
                Type mytype = edtProperty.GetType();
                MemberInfo[] ms = mytype.FindMembers(MemberTypes.Property,
                                                     BindingFlags.Instance | BindingFlags.Public,
                                                     Type.FilterName, "*");
                foreach (MemberInfo m in ms)
                {
                    foreach (Attribute att in m.GetCustomAttributes(true))
                    {
                        if (att is DataMemberAttribute)
                        {
                            if (mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0]) is int ||
                                mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0]) is UInt32)
                            {
                                op += " this.property." + m.Name +
                                      " = " + (mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0])) +
                                      " ; ";
                            }
                            else if (mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0]) is DateTime)
                            {
                                op += " this.property." + m.Name +
                                      " = Convert.ToDateTime (\"" +
                                      (mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0])) +
                                      "\") ; ";
                            }
                            else
                            {
                                op += " this.property." + m.Name +
                                      " = \"" + (string)mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0]) +
                                      "\" ; ";
                            }
                        }
                    }
                }

                #endregion

                op +=
                    @"
                                }
                                return this.property;
                            }
                        }
                        ";
                type.Members.Add(new CodeSnippetTypeMember(op));
            }

            #endregion

            #region C# TInt

            if (((EDTProperty)node.Property).EDTType == "TInt")
            {
                //增加带参数构造器
                // Declare a constructor that takes a string argument and calls the base class constructor with it.
                CodeConstructor baseStringConstructor1 = new CodeConstructor();
                baseStringConstructor1.Attributes = MemberAttributes.Public;
                // Declares a parameter of type string named "TestStringParameter".    
                baseStringConstructor1.Parameters.Add(new CodeParameterDeclarationExpression("System.Int32", "_" + name));
                // Calls a base class constructor with the TestStringParameter parameter.
                baseStringConstructor1.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("_" + name));
                // Adds the constructor to the Members collection of the DerivedType.
                type.Members.Add(baseStringConstructor1);

                //定义隐式转换
                op = @"
                public static implicit operator " + name + @" (Int32 x) { return new " + name +
                     @"(x); }
                public static explicit operator Int32(" + name +
                     @" x) {  return x.Value; }
                public static explicit operator bool(" + name +
                     @" x) { return x.Value== DefaultValue.Value; }
                ";
                type.Members.Add(new CodeSnippetTypeMember(op));
                //属性变量
                edtProperty = ((EDTProperty)node.Property);
                op =
                    @"
                    public override  EDTIntProperty Property
                    {
                        get
                        {
                            if (this.property == null)
                            {
                                this.property = new EDTIntProperty();
                            ";

                #region C# 定义属性

                //定义属性类
                Type mytype = edtProperty.GetType();
                MemberInfo[] ms = mytype.FindMembers(MemberTypes.Property,
                                                     BindingFlags.Instance | BindingFlags.Public,
                                                     Type.FilterName, "*");
                foreach (MemberInfo m in ms)
                {
                    foreach (Attribute att in m.GetCustomAttributes(true))
                    {
                        if (att is DataMemberAttribute)
                        {
                            if (mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0]) is int)
                            {
                                op += " this.property." + m.Name +
                                      " = " + (mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0])) +
                                      " ; ";
                            }
                            else if (mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0]) is DateTime)
                            {
                                op += " this.property." + m.Name +
                                      " = Convert.ToDateTime (\"" +
                                      (mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0])) +
                                      "\") ; ";
                            }
                            else
                            {
                                op += " this.property." + m.Name +
                                      " = \"" + (string)mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0]) +
                                      "\" ; ";
                            }
                        }
                    }
                }

                #endregion

                op +=
                    @"
                                }
                                return this.property;
                            }
                        }
                        ";
                type.Members.Add(new CodeSnippetTypeMember(op));
            }

            #endregion

            #region C# TDouble

            if (((EDTProperty)node.Property).EDTType == "TDouble")
            {
                //增加带参数构造器
                // Declare a constructor that takes a string argument and calls the base class constructor with it.
                CodeConstructor baseStringConstructor1 = new CodeConstructor();
                baseStringConstructor1.Attributes = MemberAttributes.Public;
                // Declares a parameter of type string named "TestStringParameter".    
                baseStringConstructor1.Parameters.Add(new CodeParameterDeclarationExpression("System.Double", "_" + name));
                // Calls a base class constructor with the TestStringParameter parameter.
                baseStringConstructor1.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("_" + name));
                // Adds the constructor to the Members collection of the DerivedType.
                type.Members.Add(baseStringConstructor1);

                //定义隐式转换
                op = @"
                public static implicit operator " + name + @" (Double x) { return new " + name +
                     @"(x); }
                public static explicit operator Double(" + name +
                     @" x) {  return x.Value; }
                public static explicit operator bool(" + name +
                     @" x) { return x.Value== DefaultValue.Value; }
                ";
                type.Members.Add(new CodeSnippetTypeMember(op));
                //属性变量
                edtProperty = ((EDTProperty)node.Property);
                op =
                    @"
                    public override  EDTRealProperty Property
                    {
                        get
                        {
                            if (this.property == null)
                            {
                                this.property = new EDTRealProperty();
                            ";

                #region C# 定义属性

                //定义属性类
                Type mytype = edtProperty.GetType();
                MemberInfo[] ms = mytype.FindMembers(MemberTypes.Property,
                                                     BindingFlags.Instance | BindingFlags.Public,
                                                     Type.FilterName, "*");
                foreach (MemberInfo m in ms)
                {
                    foreach (Attribute att in m.GetCustomAttributes(true))
                    {
                        if (att is DataMemberAttribute)
                        {
                            if (mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0]) is int)
                            {
                                op += " this.property." + m.Name +
                                      " = " + (mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0])) +
                                      " ; ";
                            }
                            else if (mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0]) is DateTime)
                            {
                                op += " this.property." + m.Name +
                                      " =Convert.ToDateTime ( \"" +
                                      (mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0])) +
                                      "\" ); ";
                            }
                            else
                            {
                                op += " this.property." + m.Name +
                                      " = \"" + (string)mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0]) +
                                      "\" ; ";
                            }
                        }
                    }
                }

                #endregion

                op +=
                    @"
                                }
                                return this.property;
                            }
                        }
                        ";
                type.Members.Add(new CodeSnippetTypeMember(op));
            }

            #endregion

            #region C# Enum

            if (((EDTProperty)node.Property).EDTType == "TEnum")
            {
                //增加带参数构造器
                // Declare a constructor that takes a string argument and calls the base class constructor with it.
                CodeConstructor baseStringConstructor1 = new CodeConstructor();
                baseStringConstructor1.Attributes = MemberAttributes.Public;
                // Declares a parameter of type string named "TestStringParameter".    
                baseStringConstructor1.Parameters.Add(new CodeParameterDeclarationExpression("System.Int32", "_" + name));
                // Calls a base class constructor with the TestStringParameter parameter.
                baseStringConstructor1.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("_" + name));

                StringBuilder sBuilder1 = new StringBuilder();
                string enumType = string.Empty;
                if (((EDTEnumProperty)node.Property).EnumType != "" && ((EDTEnumProperty)node.Property).EnumType != null)
                    enumType = ((EDTEnumProperty)node.Property).EnumType;
                else
                    enumType = ((EDTEnumProperty)node.Property).Name;

                sBuilder1.AppendFormat("this.BaseEnumType =typeof(" + Util.RootClassNameSpace + ".Enum.{0});",
                                       enumType);
                CodeSnippetStatement statement1 = new CodeSnippetStatement(sBuilder1.ToString());
                baseStringConstructor1.Statements.Add(statement1);


                // Adds the constructor to the Members collection of the DerivedType.
                type.Members.Add(baseStringConstructor1);

                //定义隐式转换
                op = @"
                public static implicit operator " + name + @" (Int32 x) { return new " + name +
                     @"(x); }
                public static explicit operator Int32(" + name +
                     @" x) { return x.Value; }
                public static explicit operator bool(" + name +
                     @" x) { return x.Value== DefaultValue.Value; }
                ";
                type.Members.Add(new CodeSnippetTypeMember(op));
                //属性变量
                edtProperty = ((EDTProperty)node.Property);
                op =
                    @"
                    public override  EDTEnumProperty Property
                    {
                        get
                        {
                            if (this.property == null)
                            {
                                this.property = new EDTEnumProperty();
                            ";

                #region C# 定义属性

                //定义属性类
                Type mytype = edtProperty.GetType();
                MemberInfo[] ms = mytype.FindMembers(MemberTypes.Property,
                                                     BindingFlags.Instance | BindingFlags.Public,
                                                     Type.FilterName, "*");
                foreach (MemberInfo m in ms)
                {
                    foreach (Attribute att in m.GetCustomAttributes(true))
                    {
                        if (att is DataMemberAttribute)
                        {
                            if (mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0]) is int)
                            {
                                op += " this.property." + m.Name +
                                      " = " + (mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0])) +
                                      " ; ";
                            }
                            else if (mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0]) is DateTime)
                            {
                                op += " this.property." + m.Name +
                                      " = Convert.ToDateTime (\"" +
                                      (mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0])) +
                                      "\" ); ";
                            }
                            else
                            {
                                op += " this.property." + m.Name +
                                      " = \"" + (string)mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0]) +
                                      "\" ; ";
                            }
                        }
                    }
                }

                #endregion

                op +=
                    @"
                                }
                                return this.property;
                            }
                        }
                        ";
                type.Members.Add(new CodeSnippetTypeMember(op));
                op =
                    @"
                //显示转换，隐式转换，以及Property属性 不改

                public override List<EnumatorProperty> EnumatorProperty
                {
                    get
                    {
                        if (this.BaseEnumType == null)
                            return null;
                        else
                        {
                            BaseEnum baseEnum =(BaseEnum ) Activator.CreateInstance(this.BaseEnumType);
                            return baseEnum.BaseEnumProperty.EnumatorPropertyList;
                        }
                    }            
                }
            ";
                type.Members.Add(new CodeSnippetTypeMember(op));
            }

            #endregion

            #region C# TContainer

            if (((EDTProperty)node.Property).EDTType == "TContainer")
            {
                //增加带参数构造器
                // Declare a constructor that takes a string argument and calls the base class constructor with it.
                CodeConstructor baseStringConstructor1 = new CodeConstructor();
                baseStringConstructor1.Attributes = MemberAttributes.Public;
                // Declares a parameter of type string named "TestStringParameter".    
                baseStringConstructor1.Parameters.Add(new CodeParameterDeclarationExpression("Container", "_" + name));
                // Calls a base class constructor with the TestStringParameter parameter.
                baseStringConstructor1.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("_" + name));
                // Adds the constructor to the Members collection of the DerivedType.
                type.Members.Add(baseStringConstructor1);

                //定义隐式转换
                op = @"
                public static implicit operator " + name + @" (Container x) { return new " +
                     name + @"(x); }
                public static explicit operator Container (" + name +
                     @" x) {  return x.Value; }
                public static explicit operator bool(" + name +
                     @" x) { return x.Value== DefaultValue.Value; }
                ";
                type.Members.Add(new CodeSnippetTypeMember(op));
                //属性变量
                edtProperty = ((EDTProperty)node.Property);
                op =
                    @"
                     public override  EDTContainerProperty Property
                    {
                        get
                        {
                            if (this.property == null)
                            {
                                this.property = new EDTContainerProperty();
                            ";

                #region C# 定义属性

                //定义属性类
                Type mytype = edtProperty.GetType();
                MemberInfo[] ms = mytype.FindMembers(MemberTypes.Property,
                                                     BindingFlags.Instance | BindingFlags.Public,
                                                     Type.FilterName, "*");
                foreach (MemberInfo m in ms)
                {
                    foreach (Attribute att in m.GetCustomAttributes(true))
                    {
                        if (att is DataMemberAttribute)
                        {
                            if (mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0]) is int)
                            {
                                op += " this.property." + m.Name +
                                      " = " + (mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0])) +
                                      " ; ";
                            }
                            else if (mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0]) is DateTime)
                            {
                                op += " this.property." + m.Name +
                                      " = Convert.ToDateTime (\"" +
                                      (mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0])) +
                                      "\" ); ";
                            }
                            else
                            {
                                op += " this.property." + m.Name +
                                      " = \"" + (string)mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0]) +
                                      "\" ; ";
                            }
                        }
                    }
                }

                #endregion

                op +=
                    @"
                                }
                                return this.property;
                            }
                        }
                        ";
                type.Members.Add(new CodeSnippetTypeMember(op));
            }

            #endregion

            #region C# TDate

            if (((EDTProperty)node.Property).EDTType == "TDate")
            {
                //增加带参数构造器
                // Declare a constructor that takes a string argument and calls the base class constructor with it.
                CodeConstructor baseStringConstructor1 = new CodeConstructor();
                baseStringConstructor1.Attributes = MemberAttributes.Public;
                // Declares a parameter of type string named "TestStringParameter".    
                baseStringConstructor1.Parameters.Add(new CodeParameterDeclarationExpression("DateTime", "_" + name));
                // Calls a base class constructor with the TestStringParameter parameter.
                baseStringConstructor1.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("_" + name));
                // Adds the constructor to the Members collection of the DerivedType.
                type.Members.Add(baseStringConstructor1);

                //定义隐式转换
                op = @"
                public static implicit operator " + name + @" (DateTime x) { return new " +
                     name + @"(x); }
                public static explicit operator DateTime(" + name +
                     @" x) { return x.Value; }
                public static explicit operator bool(" + name +
                     @" x) { return x.Value== DefaultValue.Value; }
                ";
                type.Members.Add(new CodeSnippetTypeMember(op));
                //属性变量
                edtProperty = ((EDTProperty)node.Property);
                op =
                    @"
                    public override  EDTDateProperty Property
                    {
                        get
                        {
                            if (this.property == null)
                            {
                                this.property = new EDTDateProperty();
                            ";

                #region C# 定义属性

                //定义属性类
                Type mytype = edtProperty.GetType();
                MemberInfo[] ms = mytype.FindMembers(MemberTypes.Property,
                                                     BindingFlags.Instance | BindingFlags.Public,
                                                     Type.FilterName, "*");
                foreach (MemberInfo m in ms)
                {
                    foreach (Attribute att in m.GetCustomAttributes(true))
                    {
                        if (att is DataMemberAttribute)
                        {
                            if (mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0]) is int)
                            {
                                op += " this.property." + m.Name +
                                      " = " + (mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0])) +
                                      " ; ";
                            }
                            else if (mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0]) is DateTime)
                            {
                                op += " this.property." + m.Name +
                                      " = Convert.ToDateTime (\"" +
                                      (mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0])) +
                                      "\" ); ";
                            }
                            else
                            {
                                op += " this.property." + m.Name +
                                      " = \"" + (string)mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0]) +
                                      "\" ; ";
                            }
                        }
                    }
                }

                #endregion

                op +=
                    @"
                                }
                                return this.property;
                            }
                        }
                        ";
                type.Members.Add(new CodeSnippetTypeMember(op));
            }

            #endregion

            #region C# TTime

            if (((EDTProperty)node.Property).EDTType == "TTime")
            {
                //增加带参数构造器
                // Declare a constructor that takes a string argument and calls the base class constructor with it.
                CodeConstructor baseStringConstructor1 = new CodeConstructor();
                baseStringConstructor1.Attributes = MemberAttributes.Public;
                // Declares a parameter of type string named "TestStringParameter".    
                baseStringConstructor1.Parameters.Add(new CodeParameterDeclarationExpression("System.Int32", "_" + name));
                // Calls a base class constructor with the TestStringParameter parameter.
                baseStringConstructor1.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("_" + name));
                // Adds the constructor to the Members collection of the DerivedType.
                type.Members.Add(baseStringConstructor1);

                //定义隐式转换
                op = @"
                public static implicit operator " + name + @" (Int32 x) { return new " + name +
                     @"(x); }
                public static explicit operator Int32(" + name +
                     @" x) {  return x.Value; }
                public static explicit operator bool(" + name +
                     @" x) { return x.Value== DefaultValue.Value; }
                ";
                type.Members.Add(new CodeSnippetTypeMember(op));
                //属性变量
                edtProperty = ((EDTProperty)node.Property);
                op =
                    @"
                     public override  EDTTimeProperty Property
                    {
                        get
                        {
                            if (this.property == null)
                            {
                                this.property = new EDTTimeProperty();
                            ";

                #region C# 定义属性

                //定义属性类
                Type mytype = edtProperty.GetType();
                MemberInfo[] ms = mytype.FindMembers(MemberTypes.Property,
                                                     BindingFlags.Instance | BindingFlags.Public,
                                                     Type.FilterName, "*");
                foreach (MemberInfo m in ms)
                {
                    foreach (Attribute att in m.GetCustomAttributes(true))
                    {
                        if (att is DataMemberAttribute)
                        {
                            if (mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0]) is int)
                            {
                                op += " this.property." + m.Name +
                                      " = " + (mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0])) +
                                      " ; ";
                            }
                            else if (mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0]) is DateTime)
                            {
                                op += " this.property." + m.Name +
                                      " = Convert.ToDateTime (\"" +
                                      (mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0])) +
                                      "\" ); ";
                            }
                            else
                            {
                                op += " this.property." + m.Name +
                                      " = \"" + (string)mytype.GetProperty(m.Name).GetValue(edtProperty, new Object[0]) +
                                      "\" ; ";
                            }
                        }
                    }
                }

                #endregion

                op +=
                    @"
                                }
                                return this.property;
                            }
                        }
                        ";
                type.Members.Add(new CodeSnippetTypeMember(op));
            }

            #endregion

            #region C# Relation

            op =
                @"
                    public override List<EDTRelation> Relations
                    {
                        get{
                           List<EDTRelation> ret = new List<EDTRelation>();
                            ";
            // SysTreeNode[] relations;

            node.ExpandAll();
            if (node.Nodes.Count > 0)
            {
                TreeNode tempnode = node.Nodes.Find("Relations", false).First();
                //relations = (SysTreeNode[])node.Nodes.Find("Relations", false);
                //relations = (SysTreeNode)tempnode.Nodes.;
                EDTRelationProperty edtRelationProperty;
                if (tempnode.Nodes.Count > 0)
                {
                    op += @"
                           EDTRelation edtRelation ;
                            ";
                }
                foreach (SysTreeNode relation in tempnode.Nodes)
                {
                    Util.SetPropertyInSilent = true;
                    Util.SetPropertyContent(relation);
                    Util.SetPropertyInSilent = false;
                    edtRelationProperty = (EDTRelationProperty)relation.Property;
                    op += "edtRelation.EDTName=" + "\"" + name + "\";";
                    op += "edtRelation.TableName=" + "\"" + edtRelationProperty.Table + "\";";
                    op += "edtRelation.FieldName=" + "\"" + edtRelationProperty.RelatedField + "\";";

                    op += "edtRelation.EDTType=typeof(" + name + ");";
                    if (edtRelationProperty.Table != "" && edtRelationProperty.RelatedField != "")
                    {
                        op += "edtRelation.TableType=typeof(" + edtRelationProperty.Table + ");";
                        //if (string.IsNullOrEmpty(edtRelationProperty.QueryType))
                        //{
                        //    op += "edtRelation.QueryType=typeof(" + edtRelationProperty.Table + "_Query" + ");";
                        //}
                        //else
                        //{
                        //    op += "edtRelation.QueryType=typeof(" + edtRelationProperty.QueryType + ");";
                        //}

                        op += "edtRelation.FieldType=typeof(" + edtRelationProperty.Table + "_" +
                              edtRelationProperty.RelatedField + ");";
                    }
                    else
                    {
                        op += "edtRelation.TableType= null;";
                        op += "edtRelation.FieldType= null;";
                    }
                    op += "ret.Add(edtRelation);";
                }
            }
            op +=
                @"
                               return ret;
                            }
                        }
                        ";
            type.Members.Add(new CodeSnippetTypeMember(op));

            #endregion

            #region 输出与编译

            //编译环境变量
            CSharpCodeProvider provider = new CSharpCodeProvider();
            CodeGeneratorOptions options = new CodeGeneratorOptions();
            options.BlankLinesBetweenMembers = false;
            options.IndentString = "\t";

            //编译参数
            //相当于项目下的“引用”部分
            CompilerParameters cp = new CompilerParameters();
            cp.OutputAssembly = Util.GetExtendDateTypesPath + "\\" + name;// Util.OTPath + "\\Module\\ExtendDataTypes\\" + name;
            cp.ReferencedAssemblies.Add("System.dll");
            cp.ReferencedAssemblies.Add("System.Data.dll");
            cp.ReferencedAssemblies.Add("System.Xml.dll");
            cp.ReferencedAssemblies.Add("mscorlib.dll");
            cp.ReferencedAssemblies.Add("System.Windows.Forms.dll");
            cp.ReferencedAssemblies.Add("RuleCode.Common.dll");
            cp.ReferencedAssemblies.Add("RuleCode.ObjectTree.dll");

            cp.WarningLevel = 3;


            cp.CompilerOptions = "/target:module /optimize /moduleassemblyname:ExtendDataTypes.dll"; //指定输出到DLL中的程序集？
            cp.GenerateExecutable = false;
            cp.GenerateInMemory = false;


            CSharpCodeProvider csp = new CSharpCodeProvider();
            DirectoryInfo EDTDir = new DirectoryInfo(EDTPath);
            if (!EDTDir.Exists)
            {
                EDTDir.Create();
            }
            IndentedTextWriter tw = new IndentedTextWriter(new StreamWriter(EDTPath + name + ".cs", false), "    ");
            csp.GenerateCodeFromCompileUnit(CompileUnit, tw, new CodeGeneratorOptions());

            tw.Close();

            CompilerResults cr = csp.CompileAssemblyFromDom(cp, CompileUnit);

            if (cr.Errors.Count > 0)
            {
                foreach (CompilerError ce in cr.Errors)
                {
                    Console.WriteLine(ce.ErrorNumber + ": " + ce.ErrorText);
                }
                //MessageBox.Show("Errors occoured", "Errors", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            #endregion



            //将编译 好的文件添加到项目中
            if(Util_DTE.IsAddToProjectAtCompiled )
                Util_DTE.AddGeneratedFileToProject (node, GenerateFileType.ExtendDataType );
        }

        public void compileEDTs(SysTreeNode edts)
        {
            edts.ExpandAll();

            foreach (SysTreeNode node in edts.Nodes)
            {
                //只编译今天修改过的节点
                Util.SetPropertyContent(node);
                //if (node.Property.ChangedDate.CompareTo(DateTime.Today) > 0)
                {
                    compileExtendDataType(node);
                }

            }

            //SysCompiler.CompileCSharpCode(Util.OTPath + @"\Module\ExtendDataTypes",
            //                              Util.GetLibPath + "\\RuleCode.ExtendTypes.dll");

            

        }

        private string typeNameMapping(string _yang)
        {
            if (_yang == "TString")
            {
                return "EString";
            }
            if (_yang == "TInt")
            {
                return "EInt";
            }
            if (_yang == "TInt64")
            {
                return "EInt64";
            }
            if (_yang == "TDouble")
            {
                return "EDouble";
            }
            if (_yang == "TEnum")
            {
                return "EEnum";
            }
            if (_yang == "TContainer")
            {
                return "EContainer";
            }
            if (_yang == "TDate")
            {
                return "EDate";
            }
            if (_yang == "TTime")
            {
                return "ETime";
            }

            return "";
        }
    }
}